﻿
using AngleSharp.Io;
using RndMaterialDisposalService.Attrbutes;
using RndMaterialDisposalService.Helper;
using RndMaterialDisposalService.Models;
using RndMaterialDisposalService.Models.RequestModel;
using RndMaterialDisposalService.Models.ResponseModel;
using RndMaterialDisposalService.Services;
using RndMaterialDisposalService.Services.Interface;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;

namespace RndMaterialDisposalService.Controllers
{
    /// <summary>
    /// 主数据
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly string userDecrypt = "/api/UserLoginInforDecrypt/Decrypt2";
        private readonly string encyptUserInfo = "/api/UserLoginInforDecrypt/Encypt";

        private IConfiguration configuration;
        private readonly IUserInfoService userInfoService;
        private readonly ILogger<UserController> logger;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_userInfoService"></param>
        /// <param name="_logger"></param>
        /// <param name="_configuration"></param>
        public UserController(IUserInfoService _userInfoService,
            ILogger<UserController> _logger,
            IConfiguration _configuration)
        {
            configuration = _configuration;
            userInfoService = _userInfoService;
            logger = _logger;
        }

        /// <summary>
        /// 获取人员列表
        /// </summary>
        /// <param name="keyWord"></param>
        /// <param name="pageNum"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("GetUserInfoList")]
        [PageList]
        public async Task<IActionResult> GetUserInfoList(string? keyWord, int pageNum = 1, int pageSize = 10)
        {
            var result = await userInfoService.GetUserInfoList(keyWord, pageNum, pageSize);

            var totalCount = result.TotalCount;
            var data = result.Data.Select(p => new UserSelectResp
            {
                Id = p.UserId,
                Gender = "",
                Name = new NameModel { First = p.UserFirstName ?? "", Last = p.UserLastName ?? "" },
                Email = p.Email ?? "",
                Cname = p.UserName,
                MobilePhone = p.MobilePhone,
                EmployeeNumber = p.EmployeeNumber ?? "",
                DisplayName = $"{p.EmployeeNumber} {p.UserName}"
            }).ToList();

            var userResult = ApiResponsePageList<UserSelectResp>.CreatePageListByResult(data, pageNum, pageSize, totalCount);
            return Ok(userResult);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="employeeNumber"></param>
        /// <returns></returns>
        [HttpGet("GetUserInfoDetail")]
        [PageList]
        public async Task<IActionResult> GetUserInfoDetail(string employeeNumber)
        {
            var result = await userInfoService.GetUserDetail(employeeNumber);
            var userResult = ApiResponse<UserBaseInforModel>.Success(result);
            return Ok(userResult);
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="paraStr"></param>
        /// <returns></returns>
        [HttpGet("GetUserInfo")]
        public async Task<IActionResult> GetUserInfo(string paraStr)
        {
            var CommonServiceHost = configuration.GetSection("CommonServiceHost").Value;
            var url = $"{CommonServiceHost}{userDecrypt}?loginInfor={paraStr}";
            var response = await HttpHelper.GetAsync<UserInfoResp>(url);
            return Ok(response);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userAccount"></param>
        /// <param name="functionId"></param>
        /// <returns></returns>
        [HttpGet("EncyptUserInfoByAccount")]
        public async Task<IActionResult> EncyptUserInfo(string userAccount, string functionId)
        {
            var userinfo = await userInfoService.GetUserInfoByAccount(userAccount);
            var CommonServiceHost = configuration.GetSection("CommonServiceHost").Value;
            var url = $"{CommonServiceHost}{encyptUserInfo}?userID={userinfo.UserId}&functionID={functionId}&procInstID=0";
            var response = await HttpHelper.GetAsync<string>(url);
            return Ok(new { response, userinfo });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="functionId"></param>
        /// <param name="procInstId"></param>
        /// <returns></returns>
        [HttpGet("EncyptUserInfo")]
        public async Task<IActionResult> EncyptUserInfo(string userId, string functionId, string? procInstId="")
        {
            var CommonServiceHost = configuration.GetSection("CommonServiceHost").Value;
            var url = $"{CommonServiceHost}{encyptUserInfo}?userID={userId}&functionID={functionId}&procInstID={procInstId}";
            var response = await HttpHelper.GetAsync<string>(url);
            return Ok(response);
        }

        /// <summary>
        /// 获取人员详细信息
        /// </summary>
        /// <param name="employeeNumber"></param>
        /// <returns></returns>
        [HttpGet("GetEmployeeInfo")]
        public async Task<IActionResult> GetEmployeeInfo(string employeeNumber)
        {
            var result = await userInfoService.GetEmployeeInfo(employeeNumber);
            return Ok(result);
        }

        /// <summary>
        /// 获取人员详细信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet("GetDetailsUserInfo")]
        public async Task<IActionResult> GetDetailsUserInfo(string userId)
        {
            var userInfo = await userInfoService.GetUserInfo(userId);
            return Ok(userInfo);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="employeeNumber"></param>
        /// <returns></returns>
        [HttpGet("GetUserDeptLeader")]
        public async Task<IActionResult> GetUserDeptLeader(string employeeNumber)
        {
            var userInfo = await userInfoService.GetUserDeptLeader(employeeNumber);
            return Ok(userInfo);
        }

        /// <summary>
        /// 日志测试
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        [HttpGet("TestLog")]
        public async Task<IActionResult> TestLog()
        {
            logger.LogTrace("LogTrace信息");
            logger.LogDebug("LogDebug");
            logger.LogInformation("LogInformation信息");
            logger.LogWarning("LogWarning信息");
            logger.LogError("LogError信息");
            logger.LogCritical("LogCritical信息");
            return Ok();
        }
    }
}
